package dtos

import (
	"github.com/edgexfoundry/edgex-go/internal/app/models"
	edgexDtos "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
	edgexModel "github.com/edgexfoundry/go-mod-core-contracts/v2/models"
)

type DeviceResource struct {
	edgexDtos.DBTimestamp    `json:",inline"`
	edgexDtos.DeviceResource `json:",inline"`
	Code                     string `json:"code,omitempty"`
	Id                       string `json:"id,omitempty"`
}

func (d *DeviceResource) ToDeviceResourceModel() models.DeviceResource {
	return models.DeviceResource{
		DBTimestamp: edgexModel.DBTimestamp{
			Created:  d.Created,
			Modified: d.Modified,
		},
		DeviceResource: edgexModel.DeviceResource{
			Description: d.Description,
			Name:        d.Name,
			IsHidden:    d.IsHidden,
			Tag:         d.Tag,
			Properties:  edgexDtos.ToResourcePropertiesModel(d.Properties),
			Attributes:  d.Attributes,
		},
		Code: d.Code,
		Id:   d.Id,
	}
}

func ToDeviceResources(drs []DeviceResource) []models.DeviceResource {
	deviceResources := make([]models.DeviceResource, len(drs))
	for index, dr := range drs {
		deviceResources[index] = dr.ToDeviceResourceModel()
	}
	return deviceResources
}
